use "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg02.dta", clear

label drop presvote
append using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg06.dta"
gen weight=.5

append using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg06mex.dta"
append using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg14.dta"
recode weight .=1

collapse (mean) disagreementstrong threshold votecomplement stability partyintense [weight=weight], by(presvote year)

gen str label="Ciro02" if presvote==1
replace label="Lula02" if presvote==2 & year==2002
replace label="Serra02" if presvote==4
replace label="Garotinho02" if presvote==5 
replace label="Lula06" if presvote==2 & year==2006
replace label="Alckmin06" if presvote==10 
replace label="HH06" if presvote==12 
replace label="Calderón06" if presvote==101 
replace label ="Madrazo06" if presvote==102 
replace label ="AMLO06" if presvote==103 
replace label ="Dilma14" if presvote==201
replace label ="Aécio14" if presvote==202
replace label ="Marina14" if presvote==204

gen switch=1-stability
drop stability
corr switch disagreementstrong
gen disagreementstrongsq=disagreementstrong^2
reg switch disagreementstrong disagreementstrongsq
gen switchmex=switch if presvote>100 & presvote<104
gen switchbra=switch if switchmex==.
predict xb, xb

*Figure 4.8
*Bivariate correlation
twoway (scatter xb disagreementstrong, connect(l) sort lcolor(black) m(i)) ///
(scatter switchmex disagreementstrong, mlab(label) mlabcolor(gs8) mc(gs8)) ///
(scatter switchbra disagreementstrong, mlab(label) mlabcolor(black) mc(black)) ///
,  ///
xscale(range(.1 .45)) xtitle("{bf:Mean Disagreement in the Networks of Supporters}" "(measured at campaign wave)") ytitle("{bf:Lost Supporters}" "(Percentage of candidate's campaign-wave" "supporters who did not vote for candidate)") graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) note("{it:r} = +.79") ylab(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%")
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Aggregate.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_8.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_8.pdf", as(pdf) replace 
xyz

*Partial correlation
pcorr switch disagreementstrong votecomplement 
pcorr switch disagreementstrong partyintense
xyz
reg switch threshold votecomplement partyintense
predict dvpurged, res

reg disagreementstrong threshold votecomplement partyintense
predict ivpurged, res

gen ivpurgedsq=ivpurged^2
reg dvpurged ivpurged ivpurgedsq
predict xbpurged, xb

gen ivpurgedmex=ivpurged if presvote>100 & presvote<104
gen ivpurgedbra=ivpurged if switchmex==.

corr dvpurged ivpurged
xyz
twoway (scatter xbpurged ivpurged, connect(l) sort lcolor(black) m(i)) ///
(scatter dvpurged ivpurgedbra, mlab(label) mlabcolor(black) mc(black)) ///
(scatter dvpurged ivpurgedmex, mlab(label) mlabcolor(gs8) mc(gs8)) ///
,  ///
xscale(range(0 .10)) xlab(none) ylabel(none) xtitle("{bf:Network Disagreement}" "(restrictive definition. PURGED)") ytitle("{bf:Candidate Failure to Maintain Supporters}" "(Proportion of candidate's campaign wave" "supporters who did not vote for her/him. PURGED)") graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) note("r = +.61")
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\AggregatePurged.tif", as(tif) replace
